/*
 * Copyright (c) 2019, Zhejiang uniview Technologies Co., Ltd. All rights reserved.
 * <http://www.uniview.com/>
 *------------------------------------------------------------------------------
 * Product     :
 * Module Name :
 * Date Created: 2020-04-07 10:42
 * Creator     : s04180
 * Description :
 *
 *------------------------------------------------------------------------------
 * Modification History
 * DATE        NAME             DESCRIPTION
 *------------------------------------------------------------------------------
 *
 *------------------------------------------------------------------------------
 */
package com.kyg.tjtoolmanager.net.netty.manager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.net.netty.constant.LapiUriConstat;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.util.internal.StringUtil;

import java.util.Map;
import java.util.Objects;

/**
 * HttpConnectionThread
 *
 * @author s04180
 * @Description
 */
public class RequestDeviceTestThread extends Thread {

    //终端接口地址URI
    private String api = LapiUriConstat.PEOPLE_BASIC_INFO_URI;

    //终端设备的序列号 or  响应心跳的 deviceCode
    //210235C6LC323C000074
    //210235C3R0319B000026
    private String serialNo = "";

    //请求的接口参数
    private String json = "";

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    @Override
    public void run() {
        try {
//            for (; ; ) {
//
//                //POST
//                // httpRequest =  RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.POST);
//
//                //DELETE
//                // httpRequest =RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.DELETE);
//
//                //PUT
//                // httpRequest =RequestDeviceManager.createRequestDeviceInfo(api, json,HttpMethod.PUT);
//
//                // 请求休眠10s
//                Thread.sleep(10000);
//            }
            if (!StringUtil.isNullOrEmpty(serialNo)) {
                //GET
                FullHttpRequest httpRequest = null;
                httpRequest = RequestDeviceManager.createRequestDeviceInfo(api, json, HttpMethod.GET);

                Map<String, String> map = HttpKeepAliveManager.getResponseMapByKeepAliveConnection(serialNo, httpRequest);
                if (Objects.nonNull(map)) {
                    String response = JSON.toJSONString(map, SerializerFeature.PrettyFormat);
//                    SQLiteUtils.insertLog("Device:{" + serialNo + " Response Info:\n" + response);
                    JSONObject jsonObject = JSON.parseObject(response);
                    JSONArray array = jsonObject.getJSONObject("Response").getJSONObject("Data").getJSONArray("LibList");
                    for (int i = 0; i < array.size(); i++) {
                        JSONObject lib = (JSONObject) array.get(i);
                        int type = lib.getInteger("Type");
                        if (type == 3) {
                            //员工库
                            int PersonNum = lib.getInteger("PersonNum");
                            SQLiteUtils.insertLog("工作台人员数：" + PersonNum);
//                            LiveEventBus.get(LiveEventKey.FACE_PERSON_NUM)
//                                    .post(PersonNum);
                            break;
                        }
                    }
                } else {
                    SQLiteUtils.insertLog("... ... 请求超时 ... ... ");
                }
            }
        } catch (Exception e) {
            SQLiteUtils.insertLog("{}" + e.getMessage());
        }
    }
}
